package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Position;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.services.api.IPSalaryParamService;
import com.zhitong.money.services.api.IPositionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class PositionContro {

    @Autowired
    private IPositionService positionService;

    @Autowired
    private IPSalaryParamService paramService;

    /**
    * @description 获取所有职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:39
    * @param:
    * @return:
    */
    @GetMapping("/positions")
    public AjaxResponse getAllPosition() throws Exception{
            List<Position> allPosition = positionService.findAllPosition();
            return AjaxResponse.success(allPosition);
    }

    /**
    * @description 获取部门下的所有职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:36
    * @param:
    * @return:
    */
    @GetMapping("/positions/{did}")
    public AjaxResponse getAllPositionByDid(@PathVariable("did")String did) throws Exception{
        log.info("获取部门id为:"+did);
            List<Position> positionByDid = positionService.findPositionByDid(did);
            AjaxResponse success = AjaxResponse.success(positionByDid);
            success.setMessage("获取职位成功");
            return success;
    }

    @GetMapping("/paramPosition")
    public AjaxResponse getParamPosition() throws Exception{
        List<String> allPid = paramService.findAllPid();
        if (allPid!=null && !allPid.isEmpty()){
            List<Position> positionByNoInIds = positionService.findPositionByNoInIds(allPid);
            return AjaxResponse.success(positionByNoInIds);
        }else{
            List<Position> allPosition = positionService.findAllPosition();
            return AjaxResponse.success(allPosition);
        }


    }

    /**
    * @description 获取部门下的职位以及还没有部门的职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:36
    * @param:
    * @return:
    */
    @GetMapping("/nonpositions/{did}")
    public AjaxResponse getNonPositionByDid(@PathVariable("did")String did) throws Exception{
        log.info("获取部门id为:"+did);
            List<Position> positionByDid = positionService.findNonPosition(did);
            AjaxResponse success = AjaxResponse.success(positionByDid);
            success.setMessage("获取职位成功");
            return success;
    }

    /**
    * @description 根据条件查询职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:37
    * @param:
    * @return:
    */
    @PostMapping("/positionMap")
    public AjaxResponse getPositionByMap(@RequestBody Map<String,Object> map) throws Exception{
            List<Position> positionByMap = positionService.findPositionByMap(map);
            return AjaxResponse.success(positionByMap);
    }

    /**
    * @description 根据条件分页查询职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:37
    * @param:
    * @return:
    */
    @PostMapping("/positions")
    public AjaxResponse getPositionByPage(@RequestBody Map<String,Object> map) throws Exception{
        log.info("map:"+map);
        PageInfo  positions = positionService.findPositionByPage(map);
        return AjaxResponse.success(positions);
    }

    /**
    * @description 根据id获取职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:37
    * @param:
    * @return:
    */
    @GetMapping("/position/{id}")
    public AjaxResponse getPositionById(@PathVariable("id")String id) throws Exception{
        Position positionById;
        positionById= positionService.findPositionById(id);
        return AjaxResponse.success(positionById);
    }

    /**
    * @description 添加职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:38
    * @param:
    * @return:
    */
    @RequiresPermissions({"addPosition"})
    @PostMapping("/position")
    public AjaxResponse addPosition(@RequestBody Position position) throws Exception{
        positionService.addPosition(position);
        return AjaxResponse.success("职位添加成功!");
    }

    /**
    * @description 跟新职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:38
    * @param: position
    * @return:
    */
    @RequiresPermissions({"updatePosition"})
    @PutMapping("/position")
    public AjaxResponse updatePosition(@RequestBody Position position) throws Exception{

        positionService.updatePosition(position);
        return AjaxResponse.success("职位修改成功!");
    }

    /**
    * @description 根据id删除职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:38
    * @param:
    * @return:
    */
    @RequiresPermissions({"delPosition"})
    @DeleteMapping("/position/{id}")
    public AjaxResponse delPosition(@PathVariable("id")String id) throws Exception{
        int i = positionService.delPositionById(id);
        if (i>0){
            return AjaxResponse.success("职位删除成功!");
        }
        return AjaxResponse.fail("删除失败!职位关系未删除");
    }

    @RequiresPermissions({"delPosition"})
    @PostMapping("/delpositions")
    public AjaxResponse delPositions(@RequestBody List<String> ids) throws Exception{
        if (ids==null || ids.isEmpty()){
            return AjaxResponse.fail("请选择职位");
        }
        int i = positionService.delPositionsByIds(ids);
        if (i>0){
            return AjaxResponse.success("删除成功!");
        }
        return AjaxResponse.fail("删除失败!职位关系未删除");
    }
}
